package com.hrm.attendance.service;

import com.hrm.attendance.dao.AttendanceConfigDao;
import com.hrm.attendance.dao.DeductionDictDao;
import com.hrm.attendance.dao.ExtraDutyConfigDao;
import com.hrm.attendance.dao.LeaveConfigDao;
import com.hrm.common.util.IdWorker;
import com.hrm.domain.attendance.entity.AttendanceConfig;
import com.hrm.domain.attendance.entity.DeductionDict;
import com.hrm.domain.attendance.entity.ExtraDutyConfig;
import com.hrm.domain.attendance.entity.LeaveConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ConfigurationService{

    @Autowired
    private AttendanceConfigDao attendanceConfigDao;
    @Autowired
    private LeaveConfigDao leaveConfigDao;
    @Autowired
    private DeductionDictDao deductionDictDao;
    @Autowired
    private ExtraDutyConfigDao extraDutyConfigDao;


    public AttendanceConfig getAtteConfig(String companyId, String departmentId) {
        return attendanceConfigDao.findByCompanyIdAndDepartmentId(companyId,departmentId);
    }

    public void saveAtteConfig(AttendanceConfig attendanceConfig) {
        AttendanceConfig ac = attendanceConfigDao.findByCompanyIdAndDepartmentId(attendanceConfig.getCompanyId(), attendanceConfig.getDepartmentId());
        if (ac != null){
            attendanceConfigDao.save(attendanceConfig);
        }else {
            attendanceConfig.setId(new IdWorker().nextId()+"");
            attendanceConfigDao.save(attendanceConfig);
        }
    }

    public List<LeaveConfig> getLeaveConfig(String companyId, String departmentId) {
        return leaveConfigDao.findByCompanyIdAndDepartmentId(companyId,departmentId);
    }

    public void saveLeaveConfig(LeaveConfig config) {
        List<LeaveConfig> list = leaveConfigDao.findByCompanyIdAndDepartmentId(config.getCompanyId(), config.getDepartmentId());
        if (list == null || list.size() == 0){
            config.setId(new IdWorker().nextId()+"");
            leaveConfigDao.save(config);
        }else {
            leaveConfigDao.save(config);
        }
    }

    public List<DeductionDict> getDeductionDict(String companyId, String departmentId) {
        return deductionDictDao.findByCompanyIdAndDepartmentId(companyId, departmentId);
    }

    public void saveDeductionDict(DeductionDict deduction) {
        List<DeductionDict> list = deductionDictDao.findByCompanyIdAndDepartmentId(deduction.getCompanyId(), deduction.getDepartmentId());
        if (list == null || list.size() == 0){
            deduction.setId(new IdWorker().nextId()+"");
            deductionDictDao.save(deduction);
        }else {
            deductionDictDao.save(deduction);
        }
    }

    public ExtraDutyConfig getExtDutyConfig(String companyId, String departmentId) {
        return extraDutyConfigDao.findByCompanyIdAndDepartmentId(companyId,departmentId);
    }

    public void saveExtraDutyConfig(ExtraDutyConfig extraDutyConfig) {
        ExtraDutyConfig edc = extraDutyConfigDao.findByCompanyIdAndDepartmentId(extraDutyConfig.getCompanyId(), extraDutyConfig.getDepartmentId());
        if (edc != null){
            extraDutyConfigDao.save(extraDutyConfig);
        }else {
            extraDutyConfig.setId(new IdWorker().nextId()+"");
            extraDutyConfigDao.save(extraDutyConfig);
        }
    }
}
